import gmsh
import sys
import os

# Before using any functions in the Python API, Gmsh must be initialized:
gmsh.initialize()

dim = 2

# By default Gmsh will not print out any messages: in order to output messages
# on the terminal, just set the "General.Terminal" option to 1:
gmsh.option.setNumber("General.Terminal", 1)

# Next we add a new model named "t1" (if gmsh.model.add() is not called a new
# unnamed model will be created on the fly, if necessary):
gmsh.model.add("t1")

p1_tag = gmsh.model.geo.addPoint(-2, -2, 0)
p2_tag = gmsh.model.geo.addPoint(2, -2, 0)
p3_tag = gmsh.model.geo.addPoint(2, 2, 0)
p4_tag = gmsh.model.geo.addPoint(-2, 2, 0)
p5_tag = gmsh.model.geo.addPoint(-2, 4, 0)
p6_tag = gmsh.model.geo.addPoint(2, 4, 0)

l1_tag = gmsh.model.geo.addLine(p1_tag, p2_tag)
l2_tag = gmsh.model.geo.addLine(p2_tag, p3_tag)
l3_tag = gmsh.model.geo.addLine(p3_tag, p4_tag)
l4_tag = gmsh.model.geo.addLine(p4_tag, p1_tag)
l5_tag = gmsh.model.geo.addLine(p4_tag, p5_tag)
l6_tag = gmsh.model.geo.addLine(p5_tag, p6_tag)
l7_tag = gmsh.model.geo.addLine(p6_tag, p3_tag)

s1_tag = gmsh.model.geo.addCurveLoop([1, 2, 3, 4])
s2_tag = gmsh.model.geo.addCurveLoop([-3, -7, -6, -5])

rect1_tag = gmsh.model.geo.addPlaneSurface([s1_tag])
rect2_tag = gmsh.model.geo.addPlaneSurface([s2_tag])

gmsh.model.geo.mesh.setTransfiniteCurve(1, 3)
gmsh.model.geo.mesh.setTransfiniteCurve(2, 3)
gmsh.model.geo.mesh.setTransfiniteCurve(3, 3)
gmsh.model.geo.mesh.setTransfiniteCurve(4, 3)
gmsh.model.geo.mesh.setTransfiniteCurve(5, 2)
gmsh.model.geo.mesh.setTransfiniteCurve(6, 3)
gmsh.model.geo.mesh.setTransfiniteCurve(7, 2)

gmsh.model.geo.mesh.setTransfiniteSurface(1)
gmsh.model.geo.mesh.setRecombine(2, 1)
gmsh.model.geo.mesh.setTransfiniteSurface(2)
gmsh.model.geo.mesh.setRecombine(2, 2)

dirichlet1_tag = gmsh.model.addPhysicalGroup(1, [l1_tag])
gmsh.model.setPhysicalName(1, dirichlet1_tag, "dirichlet_P1_x0_Linear_0_1")
mat1_tag = gmsh.model.addPhysicalGroup(dim, [rect1_tag])
gmsh.model.setPhysicalName(dim, mat1_tag, "mat1_DuncanChang")
mat2_tag = gmsh.model.addPhysicalGroup(dim, [rect2_tag])
gmsh.model.setPhysicalName(dim, mat2_tag, "mat2_elastic")
# ---------------------------------------------------------------
# Before it can be meshed, the internal CAD representation must be synchronized
# with the Gmsh model, which will create the relevant Gmsh data structures.
# This is achieved by the gmsh.model.geo.synchronize() API call for the
# built-in geometry kernel. Synchronizations can be called at any time, but
# they involve a non trivial amount of processing; so while you could
# synchronize the internal CAD data after every CAD command, it is usually
# better to minimize the number of synchronization points.
gmsh.model.geo.synchronize()

# We can then generate a 2D mesh...
gmsh.model.mesh.generate(dim)

# ... and save it to disk
ScriptPath = os.path.split(os.path.realpath(sys.argv[0]))[0]
gmsh.write(ScriptPath + "/../gmshParser/example.msh")

# Launch the GUI to see the results:
gmsh.fltk.run()

# This should be called when you are done using the Gmsh Python API:
gmsh.finalize()
